<?php
class LbsController
{
    private $diaryListModel;
    private $lbsModel;
    private $feedModel;
  
    function __construct(){
        
        $this->diaryListModel       =   new DiaryListModel();
        $this->lbsModel             =   new LbsModel();      
        $this->feedModel            =   new feedModel();
        $this->actions              =   array('getDiarysByCity', 'getDiarysByCityIP');
    }
   
   public function callApi($param){
       
        $ac = $param['action'];
        # check $do 
        $doCheck    =   $this->checkAction($ac);
        if (!empty(AC::$errorNo))
        {
            return  $doCheck;
        } 
    
        return $this->$ac($param);
    }
    
    /**
     * 根据城市查点滴
     * @param unknown $param
     */
    private function getDiarysByCity($param){     
        $citycode   =   $param['citycode'];
        $id         =   $param['id'];
        $length     =   $param['length'];       
        $uid        =   intval($param['uid']);
        $host_uid   =   AC::HOST_UID;
      
        if(empty($citycode) || intval($uid) < 1){
            AC::$errorNo    =   'LDC001';
            return FALSE;
        }
        
        if($host_uid != $uid){
            AC::$errorNo    =   'LDC002';
            return FALSE;
        }
        
        $cityid = $this->lbsModel->getGeoHash($citycode);  
            
        if(empty($cityid)){
            AC::$errorNo    =   'LDC003';
            return FALSE;
        }        
             
        list($id, $start, $length)  =   UK::assemblePageData($param);
        $bodyIds                    =   $this->lbsModel->getDiarysBodyIdByCity($cityid,$citycode,$id,$length);
                    
        if(empty($bodyIds)) return  array();      
        
        $result=$this->getDiarys($bodyIds);
        return $result;
    }

    /**
     * 根据城市查点滴
     * @param unknown $param
     */
    private function getDiarysByCityIP($param){  
        if($param['citycode']){
            return $this->getDiarysByCity($param);
        }
        $ip = "";
        if (getenv("HTTP_CLIENT_IP"))  
            $ip = getenv("HTTP_CLIENT_IP");  
        else if(getenv("HTTP_X_FORWARDED_FOR"))  
            $ip = getenv("HTTP_X_FORWARDED_FOR");  
        else if(getenv("REMOTE_ADDR"))  
            $ip = getenv("REMOTE_ADDR");
        if(!$ip){
            AC::$errorNo = 'LDC004';
            return FALSE;
        }
        
        $result = $this->getAreaByIp($ip);
        $province = $result['code']['parent'];
        $city = $result['code']['city'];
        $param['citycode'] = md5($province.'省'.$city.'市');

        $citys=array('北京','上海','天津','重庆');
        if(in_array($province, $citys)){
            $param['citycode']=md5($province.'市'.$province.'市'); 
        } 
        $autonomousRegions = array('新疆','广西','西藏','宁夏','内蒙古');
        if(in_array($province, $autonomousRegions)){
            switch ($province) {
                case '新疆':
                    $province = '新疆维吾尔自治区';
                    break;
                case '广西':
                    $province = '广西壮族自治区';
                    break;
                case '西藏':
                    $province = '西藏自治区';
                    break;
                case '宁夏':
                    $province = '宁夏回族自治区';
                    break;
                case '内蒙古':
                    $province = '内蒙古自治区';
                    break;
            }
            $param['citycode']=md5($province.$citys.'市'); 
        }
        return $this->getDiarysByCity($param);
    }
    
    private function getDiarys($bodyIds){
        foreach ($bodyIds    as  $list){
            $tmp          =   $this->diaryListModel->getDiaryInfoByBodyId($list['typeid']);
            if(empty($tmp)){
                continue;
            }
        
            $tmp['id']    =   $tmp['bodyId'];
            $list         =   $this->feedModel->square($tmp);
            $result[]     =   $list;
        }        
        return $result;
    }

    private function getAreaByIp($queryIP){ 
        $url = 'http://api.fenfenriji.com/geo/info?ip='.$queryIP;
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION,true);
        $responseText = curl_exec($curl);
        curl_close($curl);
        $responseText = mb_convert_encoding($responseText, 'utf-8', 'GBK,UTF-8,ASCII');
        return json_decode($responseText, true); 
    } 
    
    private function checkAction($action){
        
        if (in_array($action, $this->actions) && method_exists($this, $action))  return  TRUE;
        AC::$errorNo    =   'SA004';
        return  ' Error : call do is not exists in LBSController! ';
    }
}